package main

import (
	"strings"
)

func LengthOfLongestSubstring(s string) int {

	start := 0
	end := 0
	for i := 0; i < len(s); i++ {
		// 从start到i的字符串中查找s[i]的位置
		index := strings.Index(s[start:i], string(s[i]))
		//
		if index == -1 && (i+1) > end {
			end = i + 1
		} else {
			start += index + 1
			end += index + 1
		}

	}
	return end - start

}

func LengthOfLongestSubstring2(s string) int {

	start := 0
	end := 0
	for i := 0; i < len(s); i++ {
		// 从start到i的字符串中查找s[i]的位置
		index := strings.Index(s[start:i], string(s[i]))
		//
		if index == -1 && (i+1) > end {
			end = i + 1
		} else {
			start += index + 1
			end += index + 1
		}

	}
	return end - start

}

func main() {
	var s = "abcabcbb"
	println(LengthOfLongestSubstring(s))

	s = "bbbbb"
	println(LengthOfLongestSubstring(s))

	s = "pwwkew"
	println(LengthOfLongestSubstring(s))
}
